<?php
// +----------------------------------------------------------------------
// | A3Mall
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: xzncit <158373108@qq.com>
// +----------------------------------------------------------------------

namespace app\common\model\users;

use app\common\model\base\A3Mall;

class Recharge extends A3Mall {
    protected $autoWriteTimestamp = true;
    protected $name = "recharge";

    public const CAHNNEL_ALIPAY=1;
    public const CAHNNEL_WXPAY=2;
    public const CAHNNEL_WALLET=3;
    public const CAHNNEL_OFFLINE=4;

    public const STATUS_PENDING=0;
    public const CAHNNEL_SUCCESS=1;
    public const CAHNNEL_FAILED=2;

    protected $type = [
        "id"=>"integer",
        "charge_no"=>"string",
        "amount"=>"float",
        "channel"=>"integer",
        "user_id"=>"integer",
        "create_time"=>"integer",
        "source_account"=>"string",
        "status"=>"integer",
        "update_time"=>"integer",
    ];

    protected static $mapping=[
        self::CAHNNEL_ALIPAY=>'支付宝',
        self::CAHNNEL_WXPAY=>'微信',
        self::CAHNNEL_WALLET=>'奖励钱包',
        self::CAHNNEL_OFFLINE=>'线下充值',
    ];

    protected static $status_mapping=[
        self::STATUS_PENDING=>'待充值',
        self::CAHNNEL_SUCCESS=>'成功',
        self::CAHNNEL_FAILED=>'失败',
    ];


    public static function getChannelList()
    {
        return self::$mapping;
    }

    public static function getChannelText(int $channel)
    {
        return self::$mapping[$channel] ?? '';
    }

    public static function getStatuslList()
    {
        return self::$status_mapping;
    }

    public static function getStatusText(int $status)
    {
        return self::$status_mapping[$status] ?? '';
    }

    public static function getValidStatusList()
    {
        $ret=self::$status_mapping;
        unset($ret[static::STATUS_PENDING]);
        return $ret;
    }   

    //判断状态是否有效
    public static function isValidStatus(int $status)
    {
        return array_key_exists($status,static::getValidStatusList());
    }

    public function users(){
        return $this->hasOne(Users::class,"id","user_id")
            ->bind(['username'])->joinType("INNER");
    }

    public function getList($condition,$size=10,$page=1){
        // echo $this->withJoin(["users"])->where($condition)->fetchSql(true)->select();exit();
        $count = $this->withJoin(["users"])->where($condition)->count();
        $data = $this->withJoin(["users"])->where($condition)->order('recharge.id','DESC')->paginate($size);

        return [
            "count"=>$count,
            "data"=>array_map([$this,'format'],$data->items())
        ];
    }

   public function getCreateTimeAttr($value){
       return date("Y-m-d H:i:s",$value);
   }

   public function getUpdateTimeAttr($value){
       return date("Y-m-d H:i:s",$value);
   }

    public function getAmountAttr($value){
        return sprintf('%.2f',$value);
    }

    public function getChannelTextAttr($value,$data)
    {
        return static::getChannelText($data['channel']) ?? '';
    }
    
    public function getStatusTextAttr($value,$data)
    {
        return static::getStatusText($data['status']) ?? '';
    }


    protected function format($value)
    {
        $value->channel_name=$value->channel_text;
        $value->status_name=$value->status_text;
        return $value;
    }    

}
